package club.jdiy.core.job.config;

import club.jdiy.core.properties.JDiyProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;

/**
 * JDiy计划任务调度核心配置类．
 *
 * @author ziquee(子秋) QQ号:39886616 主页: http://www.jdiy.club
 * @version 20210622
 */
@SpringBootConfiguration
public class QrtzSchedulerConfiguration {

    private final QrtzJobFactory jdiyJobFactory;

    @Autowired
    public QrtzSchedulerConfiguration(QrtzJobFactory jdiyJobFactory) {
        this.jdiyJobFactory = jdiyJobFactory;
    }

    @Bean(name = "schedulerFactoryBean")
    public SchedulerFactoryBean schedulerFactoryBean(
            @Qualifier("dataSource") DataSource dataSource //remove this param to use the dataSource in the file:quartz.properties
    ) throws IOException {
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        if (properties.getJob().isPower()) {
            factory.setDataSource(dataSource);//disable this line to use the dataSource in the file:quartz.properties
            // quartz参数
            Properties prop = new Properties();
            prop.put("org.quartz.scheduler.instanceName", "JDiyScheduler");
            prop.put("org.quartz.scheduler.instanceId", "AUTO");
            prop.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
            prop.put("org.quartz.threadPool.threadCount", "50");
            prop.put("org.quartz.threadPool.threadPriority", "5");
            prop.put("org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread", "true");
            prop.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
            prop.put("org.quartz.jobStore.isClustered", "true");
            prop.put("org.quartz.jobStore.clusterCheckinInterval", "15000");
            prop.put("org.quartz.jobStore.maxMisfiresToHandleAtATime", "1");
            prop.put("org.quartz.jobStore.txIsolationLevelSerializable", "true");
            prop.put("org.quartz.jobStore.misfireThreshold", "5000");
            prop.put("org.quartz.jobStore.tablePrefix", "QRTZ_");
            factory.setQuartzProperties(prop);
            factory.setSchedulerName("JDiyScheduler");
            // 延时启动
            factory.setStartupDelay(3);
            factory.setApplicationContextSchedulerContextKey("JDiySchedulerContext");
            // 可选，QuartzScheduler
            // 启动时更新己存在的Job，这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了
            factory.setOverwriteExistingJobs(true);
            // 设置自动启动，默认为true
            factory.setAutoStartup(true);

            try {
                factory.setGlobalJobListeners(jobListener);
                //JDiy triggerListener暂未使用
                //factory.setGlobalTriggerListeners(new QrtzTriggerListener("TimerTrigger"));
                //factory.setSchedulerListeners(..);
            } catch (Exception ex) {
                ex.printStackTrace();
            }

            factory.setJobFactory(jdiyJobFactory);

        }
        return factory;

    }

    @Resource
    private JDiyProperties properties;
    @Resource
    private QrtzJobListener jobListener;

}